package sub

import (
	"encoding/json"
	"log"
	MQ "seckill-srv/basic/init"
	"seckill-srv/handler/model"
)

func OrderSub() {
	rabbitmq := MQ.NewRabbitMQSimple("2301a_seckill")
	var order model.Order
	msgs, _ := rabbitmq.ConsumeSimple()

	forever := make(chan bool)
	//启用协程处理消息
	go func() {
		for d := range msgs {
			//消息逻辑处理，可以自行设计逻辑
			log.Printf("Received a message: %s", d.Body)
			_ = json.Unmarshal(d.Body, &order)
			//创建订单
			if err := order.AddOrder(); err != nil {
				//回滚库存

				return
			}

		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
}
